package com.lvkerry.orderfood.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lvkerry.orderfood.entity.WxDevelopIdEntity;
import com.lvkerry.orderfood.provider.WxDevelopIdStore;
import com.lvkerry.orderfood.repository.WxDevelopIdRepository;
import com.lvkerry.orderfood.service.WxApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

import static com.lvkerry.orderfood.global.WxConstKt.WX_ACCESS_TOKEN_URL;

/**
 * @author Kerry
 */

@Service
public class WxApiServiceImpl implements WxApiService {

    private static Logger logger = LoggerFactory.getLogger(WxApiServiceImpl.class);

    private final RestTemplate restTemplate;
    private final WxDevelopIdStore wxDevelopIdStore;
    private final WxDevelopIdRepository wxDevelopIdRepository;
    private final ObjectMapper objectMapper;

    @Autowired
    public WxApiServiceImpl(RestTemplateBuilder restTemplateBuilder, WxDevelopIdStore wxDevelopIdStore, WxDevelopIdRepository wxDevelopIdRepository, ObjectMapper objectMapper) {
        this.restTemplate = restTemplateBuilder.build();
        this.wxDevelopIdStore = wxDevelopIdStore;
        this.wxDevelopIdRepository = wxDevelopIdRepository;
        this.objectMapper = objectMapper;
    }


    @Override
    public String getWxAccessToken() {
//        WxDevelopIdEntity developId = wxDevelopIdRepository.findTopByOrderByLastTimeDesc();
//
//        ResponseEntity<String> resultEntity = restTemplate.getForEntity(WX_ACCESS_TOKEN_URL, String.class, developId.getGrantType(), developId.getAppId(), developId.getSecret());
//        if (resultEntity != null) {
//            if (resultEntity.getStatusCode() == HttpStatus.OK) {
//                logger.info(resultEntity.getBody());
//                try {
//                    JsonNode node = objectMapper.readTree(resultEntity.getBody());
//                    JsonNode errCodeNode = node.get("errcode");
//                    if (errCodeNode == null) {
//                        long expiresIn = node.get("expires_in").asLong();
//                        String accessToken = node.get("access_token").textValue();
//                        wxDevelopIdStore.setCache(accessToken, expiresIn);
//                        return accessToken;
//                    }
//                    logger.info(resultEntity.getBody());
//                    //int errCode = errCodeNode.asInt();
//                    //String errmsg = node.get("errmsg").textValue();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//            logger.info("http error code {}", resultEntity.getStatusCodeValue());
//        }
        return null;
    }
}
